/********************************************************************************
Name:  HF_SearchController
Copyright © 2012  HFH International
======================================================
======================================================
Purpose:
-------
Search for contacts in PIMS and import 
======================================================
======================================================
History 
-------
VERSION  AUTHOR                DATE        DETAIL              
  1.0  - CG DEV TEAM    6/21/2012   INITIAL DEVELOPMENT  
*********************************************************************************/ 
public with sharing class HF_SearchController {

 public HF_SearchController(ApexPages.StandardController controller) {
    
        contact_address=new Contact_Address__c();
        out=false;
        Iseditform=true;
    }

     public Boolean Iseditform{set;get;}
     public HF_wrapperClass.SearchWrapper searchwrapper{set;get;}
     public List<HF_wrapperClass.SearchWrapper> searchwrapper2{set;get;}
     public List<HF_wrapperClass.SearchWrapper> searchwrapper3{set;get;}
     public List<WebServiceFinal2.SearchContactWrapper> searchresults{set;get;} 
     public List<Contact> contactDetail{get;set;}
     public List<Contact> selectedContact{get;set;}
     public Contact_Address__c contact_address {get;set;}
     private String soql {get;set;}
     public Id Id{set;get;}
     public String conId{set;get;}
     public String cid{set;get;}
     public String firstname{get;set { firstname=value ==''? null: value ; }}
     public String lastname {get;set { lastname=value ==''? null: value ; }}
     public String email{get;set { email=value ==''? null: value ; }}
     public String phone{get;set { phone=value ==''? null: value ; }}
     public String state{get;set { state=value ==''? null: value ; }}
     public String zipcode{get;set { zipcode=value ==''? null: value ; }}
     public String city{get;set { city=value ==''? null: value ; }}
     public String streetaddress{get;set { streetaddress=value ==''? null: value ; }}
     public String country{get;set { country=value ==''? null: value ; }}
     public Boolean show{set;get;}
     public Boolean out{set;get;}
     public Boolean err{set;get;}
      public String nameId{set;get;}
      public String pimsId{set;get;}
      
      public void searchContact() {
               
              HF_wrapperClass.SearchWrapper searchDetails = new HF_wrapperClass.SearchWrapper();
              searchwrapper2=new List<HF_wrapperClass.SearchWrapper>();
              searchwrapper3=new List<HF_wrapperClass.SearchWrapper>();
              HF_ClientWSSearch pimsCall=new HF_ClientWSSearch();
              searchresults=new List<WebServiceFinal2.SearchContactWrapper>();
               /*
                 searchDetails.firstname = firstname;
                 searchDetails.lastname = lastname;
                 searchDetails.email = email;
                 searchDetails.phone = phone;
                 searchDetails.state = contact_address.State__c;
                 searchDetails.country = contact_address.Country__c;
                 searchDetails.city = city;
                 searchDetails.zipCode = zipcode;
                 searchDetails.StreetAddress = streetaddress;*/
           try{           
                   // system.debug('********searchDetails******'+searchDetails);  
             if(firstname!=null || lastname!=null || email!=null || phone!=null || streetaddress!=null || city!=null || zipcode!=null || contact_address.Country__c!=null || contact_address.State__c!=null){                       
              searchresults=pimsCall.Searchcontactinpims(firstname,lastname,email,phone,streetaddress,city,zipcode,contact_address.Country__c,contact_address.State__c);
              
              system.debug('*****searchresults****** '+searchresults);
             
              Set<String> sfdcpartnerid=new Set<String>();
              if(searchresults !=null && searchresults.size()>0){   
               system.debug('INSIDELOOP');
                   out=true;
                   for(WebServiceFinal2.SearchContactWrapper searchresultid:searchresults){
                        sfdcpartnerid.add(searchresultid.PartnerID);
                   }  
                    system.debug('@@sfdcpartnerid '+sfdcpartnerid);
                      
                  List<contact> sfdccontact=[select id,Partner_Contact_ID__c,Email,FirstName,LastName,Name_Id__c from contact where Partner_Contact_ID__c IN: sfdcpartnerid];  
                   system.debug('@@sfdccontact '+sfdccontact);
                     
                      
                      for(WebServiceFinal2.SearchContactWrapper searchresult:searchresults) {
                          searchwrapper=new HF_wrapperClass.SearchWrapper();
                          
                          if(sfdccontact!=null && sfdccontact.size()>0){                         
                          for(contact sfdccon: sfdccontact){ 
                          
                          system.debug('^^^'+searchresult.PartnerID);  
                          system.debug('$$$'+sfdccon.Partner_Contact_ID__c); 
                                               
                              if(searchresult.PartnerID==sfdccon.Partner_Contact_ID__c){
                                    system.debug('INSIDEIF');
                                    searchwrapper.email=sfdccon.Email;
                                    searchwrapper.firstname=sfdccon.FirstName;
                                    searchwrapper.lastname=sfdccon.LastName;
                                    
                                    if(sfdccon.Name_Id__c !=null){
                                        searchwrapper.nameID=String.valueOf(sfdccon.Name_Id__c);
                                    }
                                    searchwrapper.partnerID=searchresult.PartnerID;
                                    searchwrapper.phone=searchresult.Phone;
                                    searchwrapper.state=searchresult.State;
                                    searchwrapper.city=searchresult.City;
                                    searchwrapper.StreetAddress=searchresult.StreetAddress;
                                    searchwrapper.zipCode=searchresult.ZipCode;
                                    searchwrapper.contactid=sfdccon.id;
                                    searchwrapper.isview=true;
                                    searchwrapper.isimport=false;
                                    searchwrapper2.add(searchwrapper);
                                    
                  
                              }else{
                                   system.debug('INSIDEELSE');
                                    searchwrapper.email=searchresult.Email;
                                    searchwrapper.firstname=searchresult.FirstName;
                                    searchwrapper.lastname=searchresult.LastName;
                                    searchwrapper.nameID=String.valueOf(searchresult.NameID);
                                    searchwrapper.partnerID=searchresult.PartnerID;
                                    searchwrapper.phone=searchresult.Phone;
                                    searchwrapper.state=searchresult.State;
                                    searchwrapper.city=searchresult.City;
                                    searchwrapper.StreetAddress=searchresult.StreetAddress;
                                    searchwrapper.zipCode=searchresult.ZipCode;
                                    searchwrapper.isview=false;
                                    searchwrapper.isimport=true;
                                    searchwrapper2.add(searchwrapper);                                                                                         
                              }                          
                          }                          
                         }else{                                                 
                                    system.debug('INSIDEELSE');
                                    searchwrapper.email=searchresult.Email;
                                    searchwrapper.firstname=searchresult.FirstName;
                                    searchwrapper.lastname=searchresult.LastName;
                                    searchwrapper.nameID=String.valueOf(searchresult.NameID);
                                    searchwrapper.partnerID=searchresult.PartnerID;
                                    searchwrapper.phone=searchresult.Phone;
                                    searchwrapper.state=searchresult.State;
                                    searchwrapper.city=searchresult.City;
                                    searchwrapper.StreetAddress=searchresult.StreetAddress;
                                    searchwrapper.zipCode=searchresult.ZipCode;
                                    searchwrapper.isview=false;
                                    searchwrapper.isimport=true;
                                    searchwrapper2.add(searchwrapper); 
                                  }                                           
                      }
                      Integer count=0;
                      for(HF_wrapperClass.SearchWrapper s:searchwrapper2){
                        if(count<100){
                            searchwrapper3.add(s);
                            count++;
                        }
                        else
                            break;
                      }
                      
                      ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.INFO,'Showing '+searchwrapper3.size()+' results out of '+searchresults.size());
                      ApexPages.addMessage(myMsg); 
                      
               }              
               else
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,'No Results Found..')); 
             }else
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,'Please enter a key word and search again.'));                        
               }catch(Exception e){
               system.debug('not found any record'+e);
               
               }
             
           
          //return null;
    }

 public PageReference importContact() {
             system.debug('nameId#########:'+nameId);
              system.debug('pimsid#########'+pimsId);
               Integer nameID=Integer.Valueof(nameId);
            WebServiceFinal2.GetContactWrapper wrpcls=new WebServiceFinal2.GetContactWrapper();
            HF_ClientWSImport test=new HF_ClientWSImport();
            WebServiceFinal4.BasicEndpoint pims=new WebServiceFinal4.BasicEndpoint();
            WebServiceFinal2.GetContactWrapper getwr= new WebServiceFinal2.GetContactWrapper();
            getwr=pims.GetContactDetails(pimsId,nameID);
            system.debug('#######RelationshipInfo######'+getwr.RelationshipInfo);
            system.debug('#######AddressInfo######'+getwr.AddressInfo);
            system.debug('#######ContactInfo######'+getwr.ContactInfo);
            system.debug('#######EmailInfo######'+getwr.EmailInfo);
            system.debug('#######PhoneInfo######'+getwr.PhoneInfo);
            
            String result=test.InsertWrapperRecords(getwr);
            system.debug(result);
            PageReference pageRef = ApexPages.currentPage();
            
            if(result=='Record inserted'){
                Contact con=[select Id from Contact where Partner_Contact_ID__c =:pimsId];
                pageRef = new ApexPages.StandardController(con).view();
                pageRef.setRedirect(true);
        
            }
            else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,result)); 
            pageRef=null;
            }
        return pageRef;
       // return null;
    }  
                      
   
}